package hivi.vn.hivigold.adapters;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.objects.Partner;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PartnerAdapter extends BaseAdapter {
	ArrayList<Partner> arr;
	Context context;
	public PartnerAdapter(Context context, ArrayList<Partner> arr) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.arr = arr;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr==null?0:arr.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arr==null?null:arr.get(arg0);
	}
	
	public Partner getItemDirectly(int arg0)
	{
		return arr.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View v = arg1;
		Holder h;
		if(v == null)
		{
			v =( (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.partnerrow_layout, null);
			h = new Holder();
			h.tvID = (TextView)v.findViewById(R.id.tvPartnerID);
			h.tvName = (TextView)v.findViewById(R.id.tvPartnerName);
			h.tvTel = (TextView)v.findViewById(R.id.tvPartnerPhone);
			h.tvAdd = (TextView)v.findViewById(R.id.tvPartnerAddress);
			h.tvMail = (TextView)v.findViewById(R.id.tvPartnerMail);
			//h.tvNote = (TextView)v.findViewById(R.id.tvPartnerNote);
			/*h.tvImage = (ImageView)v.findViewById(R.id.imgPartner);*/
			v.setTag(h);
		}else
		{
			h = (Holder)v.getTag();
		}
		if (arr!=null)
		{
			Partner p = arr.get(arg0);
			h.tvID.setText(p.getPartnerID());
			h.tvName.setText(p.getPartnerName());
			/*Bitmap bm = Formats.creatBitmap(context,p.getPartnerImage());*/
			/*if(bm!=null)
			{
				h.tvImage.setImageBitmap(bm);
			}else
			{
				h.tvImage.setImageResource(R.drawable.nophoto);
			}*/
			h.tvMail.setText(p.getPartnerMail());
			h.tvAdd.setText(p.getPartnerAddress());
			//h.tvNote.setText(p.getPartnerNote());
			h.tvTel.setText(p.getPartnerTel());
		}
		return v;
	}

	public void swaper(ArrayList<Partner> arr)
	{
		this.arr = arr;
		notifyDataSetChanged();
	}
	
	public Partner getPartnerAtIndex(int index)
	{
		return arr.get(index);
	}
	
	static class Holder
	{
		TextView tvID;
		TextView tvName;
		TextView tvAdd;
		TextView tvTel;
		TextView tvMail;
		//TextView tvNote;
		//ImageView tvImage;
	}

}
